# -*- coding: utf-8 -*-
# @Time : 2024/3/4 21:27
# @Author : TH
# Description:完成所有excel测试用例读取
from common.OperationExcelSheet import OperationExcel
from common.Read_Yaml import ReadYaml
from config.readConfig import ReadConfig


class ReadTestCase:
    def __init__(self):
        # 获取当前excel的workbook对象
        self.open_excel = OperationExcel()
        # 获取配置文件对象
        self.cf = ReadConfig()
        # 获取读取yaml对象
        self.read_yaml = ReadYaml()

    # 读取excel测试用例编号（ID）
    def get_caseId(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_ID")
        __case_id = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        return __case_id

    # 读取excel测试用例名称
    def get_caseName(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_NAME")
        __case_name = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        return __case_name

    # 读取excel测试用例前置条件
    def get_caseCondition(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_CONDITION")
        __case_condition = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        return __case_condition

    # 读取excel测试用例中的请求方法
    def get_caseMethod(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_METHOD")
        __case_method = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        return __case_method

    # 读取excel测试用例中的请求路径
    def get_caseUrl(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_URL")
        # 配置文件获取协议
        protocol = self.cf.read_ini("HTTP", "protocol")
        # 配置文件获取域名
        url = self.cf.read_ini("HTTP", "ip")
        # 配置文件获取端口
        port = self.cf.read_ini("HTTP", "port")
        case_url = protocol + "://" + url + ":" + port + str(self.open_excel.from_ab_get_data(sheet_name, cell, row))
        return case_url

    # 读取excel测试用例中的请求参数
    def get_caseParams(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_PARAMS")
        case_params_key = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        __case_params = self.read_yaml.get_yaml_params(case_params_key)[0]
        return __case_params

    # 读取excel测试用例中的预期结果
    def get_caseExpect(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_EXPECT")
        case_expect_key = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        __case_expect = self.read_yaml.get_yaml_expect(case_expect_key)[0]
        return __case_expect

    # 读取excel测试用例中的是否执行
    def get_caseExecute(self, sheet_name, row):
        cell = self.cf.read_ini("EXCEL", "CASE_ISEXECUTE")
        __case_execute = self.open_excel.from_ab_get_data(sheet_name, cell, row)
        return __case_execute

    # 获取excel最大行
    def get_max_row(self, sheet_name):
        return self.open_excel.open_excel_sheet(sheet_name).max_row


if __name__ == '__main__':
    t = ReadTestCase()
    print(t.get_caseId('Sheet1', 2))
    print(t.get_caseName('Sheet1', 2))
    print(t.get_caseCondition('Sheet1', 2))
    print(t.get_caseMethod('Sheet1', 2))
    print(t.get_caseUrl('Sheet1', 2))
    print(t.get_caseParams('Sheet1', 2))
    print(t.get_caseExpect('Sheet1', 2))
    print(t.get_caseExecute('Sheet1', 2))
    print(t.get_max_row('Sheet3'))